﻿Public Class FreezePaymentsForm

    Private Sub FreezePaymentsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'load the boxes

        LoadPeriodBox(PeriodBox)
        PeriodBox.SelectedValue = CurrentPeriodID
        ProviderTypeBox.SelectedIndex = 0

    End Sub

    Private Sub LoadMonthBox()

        Dim sql As String = ""

        Try
            sql = "select distinct Payment_Date from " & _
                "dbo.Payment_Types pt, " & _
                "dbo.Payment_Dates pd " & _
                "where Payment_type = 'A' " & _
                "and pd.Payment_ID = pt.Payment_ID " & _
                "and Period_ID = " & PeriodBox.SelectedValue
        Catch ex As Exception
            'period box is not ready yet
            Exit Sub
        End Try
        
        LoadComboBox(MonthBox, sql, "Payment_Date", "Payment_Date")

    End Sub

    Private Sub PeriodBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PeriodBox.SelectedIndexChanged
        LoadMonthBox()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'freeze the payments

        If MonthBox.SelectedValue Is Nothing Then
            Exit Sub
        End If

        'first check if there have been payments made for this month already

        Dim TheDate As DateTime
        Dim sql As String
        Dim TheView As DataView
        Dim TheError As String = ""

        TheDate = MonthBox.SelectedValue

        Dim AcadeSQL As String
        If ProviderTypeBox.SelectedIndex = 0 Then
            AcadeSQL = "select RID from all_provider_fields where provider_type_code <> 'Acade'"
        Else
            AcadeSQL = "select RID from all_provider_fields where provider_type_code = 'Acade'"
        End If

        sql = "select * from payments where Month = " & TheDate.ToOADate - 2 & " and RID in (" & AcadeSQL & ") and Period_ID = " & PeriodBox.SelectedValue
        ShowLoadingForm()

        TheView = LoadDataView(sql)

        HideLoadingForm()

        If TheView.Count <> 0 Then
            If MessageBox.Show("Payments have already been frozen for this month, are you sure you want to freeze again?  Any allocation lines that have already been frozen will not be changed.", "Freeze again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
        End If

        If MessageBox.Show("Are you sure you want to run the freeze?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        ShowLoadingForm()

        'insert £0 payments to all current payments which do not have a payment yet into the previous months
        sql = "insert into payments " & _
                "select a.Period_ID, a.RID, a.Allocation_ID, " & Now.ToOADate - 2 & " as Payment_Date, p.Month, 0 as Month_Total " & _
                "from  " & _
                "allocations a " & _
                "inner join " & _
                "patterns_active p on (p.Period_ID = a.Period_ID and p.Allocation_ID = a.Allocation_ID and p.RID = a.RID) " & _
                "left outer join " & _
                "payments pay on (pay.period_Id = a.Period_ID and pay.RID = a.RID and pay.Allocation_ID = a.Allocation_ID and pay.Month = p.Month) " & _
                "where a.period_ID = " & PeriodBox.SelectedValue & " " & _
                "and a.status = 'Approved' " & _
                "and p.Month < " & TheDate.ToOADate - 2 & " " & _
                "and pay.rid is null " & _
                "and a.RID in (" & AcadeSQL & ") "

        TheError = RunNonQuery(sql)

        If TheError <> "" Then
            MessageBox.Show(TheError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        'now insert the profiled payment into the payments table for the selected month
        sql = "insert into Payments " & _
                "select  " & _
                "pp.Period_ID, pp.RID, pp.Allocation_ID, " & Now.ToOADate - 2 & " as Payment_Date, pp.Month, pp.Month_Total " & _
                " from dbo.PaymentProfilesV2 pp " & _
                " left outer join " & _
                " payments p on (p.period_id = pp.period_ID and p.rid = pp.RID and p.Allocation_ID = pp.Allocation_ID and p.Month = pp.Month) " & _
                "where pp.period_id = " & PeriodBox.SelectedValue & " " & _
                "and pp.month = " & TheDate.ToOADate - 2 & " " & _
                "and p.rid is null " & _
                "and pp.RID in (" & AcadeSQL & ") "

        TheError = RunNonQuery(sql)

        If TheError <> "" Then
            MessageBox.Show(TheError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        HideLoadingForm()

        MessageBox.Show("The payments have been frozen.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)


    End Sub
End Class